#pragma once
#include <fstream>
#include <vector>
#include <sstream>

#include "World.h"
#include "Vector2.h"

class Gnuplot;

class Grapher
{
public:
	Grapher(void);
	Grapher(std::string filename,
			double xmin = -200.0, 
			double xmax = 200.0, 
			double ymin = -200.0, 
			double ymax = 200.0, 
		    int vectorsBeforeFrame = -1,
			bool unset=true,
			std::string title = "");

	~Grapher(void);

	void startNewFile(std::string filename, 
					  double xmin = -200.0, 
					  double xmax = 200.0, 
					  double ymin = -200.0, 
					  double ymax = 200.0, 
					  int vectorsBeforeFrame = -1,
					  bool unset=true,
					  std::string title = "");

	void closeFile();

	void addVector(Vector2 from, Vector2 to, int linetype=-1, int width=1);

	void writeFrame(bool clear=true);

	void displayAnimation();

	void cmd(std::string cmdstr);

	void setMaxVectors(int maxVectors);

	void setUnSet(bool unset);

	void setTime(double time);

	void clearVertexBuffer();

	void WriteOutFinal(string Filename, std::vector<double*> rays);
private:
	void initGraphFile(double xmin = -200.0, 
					   double xmax = 200.0, 
					   double ymin = -200.0, 
					   double ymax = 200.0, 
					   std::string title = "");

	void drawBox(Vector2 pos, int color);
	void drawTriangle(Vector2 pos, int color);

	void parseWorldData();

	int currentVectors;
	int vectorsBeforeFrame;
	bool unset;

	std::string file;
	std::string file_flags;
	std::string file_enemyTanks;
	std::string file_yourTanks;

	double time;

	Gnuplot* plot;
	std::ofstream out;

	std::ostringstream staticCommand;
	std::ostringstream vectorsCommand;
	std::ostringstream standard;
};

